package com.tbit.uqbike.service.business.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.*;
import com.tbit.uqbike.object.pojo.vo.OpSuggestionStatVO;
import com.tbit.uqbike.service.business.SuggestionService;
import com.tbit.uqbike.webmanager.dao.core.SuggestionDao;
import com.tbit.uqbike.webmanager.dao.core.UsermsgDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service("suggestionService")
public class SuggestionServiceImpl implements SuggestionService {
    @Autowired
    private SuggestionDao suggestionDao;
    @Resource
    private UsermsgDao usermsgDao;

    @Override
    public void insert(Suggestion suggestion) {
        suggestionDao.insert(suggestion);
    }

    @Override
    public Suggestion getBySuggestionId(Integer suggestionId) {
        return suggestionDao.getBySuggestionId(suggestionId);
    }

    @Override
    public List<Suggestion> getByAccountId(Integer accountId) {
        return suggestionDao.getByAccountId(accountId);
    }

    @Override
    public List<Suggestion> getByUserId(Integer userId) {
        return suggestionDao.getByUserId(userId);
    }

    @Override
    public void handle(Suggestion suggestion, Integer isSend, Integer type) {
        suggestionDao.handle(suggestion);
        if (1 == isSend) {
            /**
             * 客户处理产生消息用于用户端展示
             * */
            suggestion = suggestionDao.getBySuggestionId(suggestion.getSuggestionId());
            Usermsg usermsg = new Usermsg();
            usermsg.setMsgtype(0);
            usermsg.setUserid(suggestion.getUserId());
            usermsg.setCreatetime(suggestion.getHandleTime());
            usermsg.setMsgstate(0);
            usermsg.setTitle(suggestion.getTitle());
            usermsg.setContent(StringUtils.defaultString(suggestion.getReply()));
            usermsg.setRemark(suggestion.getRemark());
            usermsgDao.insertSelective(usermsg);
        }
    }

    @Override
    @Transactional
    public void batchHandle(List<Integer> suggestionIdList, Integer isSend, String reply, Integer type) {
        /**判断是否同种标题*/
        boolean isEqualTitle = suggestionDao.isEqualTitle(suggestionIdList, type);
        if (!isEqualTitle) {
            throw new BaseException("请选择同类型的工单进行批量处理");
        }
        if (type == 1){
            List<Suggestion> updateSuggestionList = new ArrayList<>();
            for (Integer suggestionId : suggestionIdList){
                Suggestion suggestion = new Suggestion();
                suggestion.setSuggestionId(suggestionId);
                suggestion.setHandleTime(new Date());
                suggestion.setState(1);
                suggestion.setReply(reply);
                suggestion.setAddTime(new Date());
                updateSuggestionList.add(suggestion);
            }
            suggestionDao.batchHandleClient(updateSuggestionList);
            if (1 == isSend) {
                List<Suggestion> suggestionList = suggestionDao.getClientSuggestionByIdList(suggestionIdList);
                for (Suggestion suggestion : suggestionList){
                    /**
                     * 客户处理产生消息用于用户端展示
                     * */
                    Usermsg usermsg = new Usermsg();
                    usermsg.setMsgtype(0);
                    usermsg.setUserid(suggestion.getUserId());
                    usermsg.setCreatetime(suggestion.getHandleTime());
                    usermsg.setMsgstate(0);
                    usermsg.setTitle(suggestion.getTitle());
                    usermsg.setContent(StringUtils.defaultString(suggestion.getReply()));
                    usermsg.setRemark("");
                    usermsgDao.insertSelective(usermsg);
                }
            }
        }else {
            List<AccountSuggestion> updateSuggestionList = new ArrayList<>();
            for (Integer suggestionId : suggestionIdList){
                AccountSuggestion suggestion = new AccountSuggestion();
                suggestion.setSuggestionId(suggestionId);
                suggestion.setHandleTime(new Date());
                suggestion.setAddTime(new Date());
                suggestion.setState(1);
                updateSuggestionList.add(suggestion);
            }
            suggestionDao.batchHandleMain(updateSuggestionList);
        }
    }

    @Override
    public void handleByAccound(AccountUserSug accountUserSug) {
        suggestionDao.handleByaccount(accountUserSug);
    }

    @Override
    public Integer queryCount(Integer state, Long startTime, Long endTime, List<Integer> accountIdList, String phone, String title) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountIdList", accountIdList);
        params.put("state", state);
        params.put("phone", phone);
        params.put("title", title);
        if (startTime != null) {
            params.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            params.put("endTime", new Date(endTime));
        }
        return suggestionDao.queryCount(params);
    }

    @Override
    public List<Suggestion> queryPage(Integer state,
                                      Long startTime, Long endTime, Integer rowCount,
                                      Integer pageNO, String sortName, List<Integer> accountIdList, String phone, String title) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountIdList", accountIdList);
        params.put("state", state);
        params.put("startTime", new Date(startTime));
        params.put("endTime", new Date(endTime));
        params.put("start", (pageNO - 1) * rowCount);
        params.put("phone", phone);
        params.put("rowCount", rowCount);
        if (sortName != null) {
            params.put("sortName", sortName);
        }
        params.put("title", title);
        List<Suggestion> queryPageList = suggestionDao.queryPage(params);
        if (!CollectionUtils.isEmpty(queryPageList)) {
            for (Suggestion suggestion : queryPageList) {
                if (suggestion.getUrl() != null) {
                    suggestion.setImages(suggestion.getUrl());
                }
            }
        }
        return queryPageList;
    }

    @Override
    public Integer getCountByTime(Integer accountId, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountId", accountId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);

        return suggestionDao.getCountByTime(params);
    }

    @Override
    public List<SuggestionExport> export(List<Integer> accountIdList, Long startTime, Long endTime, Integer state, Integer type,
                                         String phone, String title) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountIdList", accountIdList);
        params.put("phone", phone);
        if (startTime != null) {
            params.put("startTime", new Date(startTime));
        }
        params.put("state", state);
        params.put("title", title);
        if (endTime != null) {
            params.put("endTime", new Date(endTime));
        }

        if (type == 0) {
            return suggestionDao.export(params);
        } else if (type == 1) {
            return suggestionDao.exportByAccount(params);
        }
        return null;
    }

    @Override
    public List<AccountSuggestion> queryPageByAccount(Integer state, Long startTime, Long endTime, Integer rowCount,
                                                      Integer pageNO, String sortName, List<Integer> accountIdList,
                                                      String phone, String title) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountIdList", accountIdList);
        params.put("state", state);
        params.put("phone", phone);
        params.put("startTime", new Date(startTime));
        params.put("endTime", new Date(endTime));
        params.put("start", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);
        if (sortName != null) {
            params.put("sortName", sortName);
        }
        params.put("title", title);
        return suggestionDao.queryPageByAccount(params);
    }

    @Override
    public Integer queryCountByAccount(Integer state, Long startTime, Long endTime, List<Integer> accountIdList,
                                       String phone, String title) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountIdList", accountIdList);
        params.put("state", state);
        params.put("phone", phone);
        params.put("title", title);
        if (startTime != null) {
            params.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            params.put("endTime", new Date(endTime));
        }
        return suggestionDao.queryCountByAccount(params);
    }

    @Override
    public PageInfo<SuggestionStat> getStatPage(Integer accountId, Integer rowCount, Integer pageNO, Integer state, Long startTime, Long endTime, Integer type) {
        PageHelper.startPage(pageNO, rowCount);
        List<SuggestionStat> list = getStatList(accountId, state, startTime, endTime, type);
        return new PageInfo<>(list);
    }

    @Override
    public List<SuggestionStat> getStatList(Integer accountId, Integer state, Long startTime, Long endTime, Integer type) {
        if (type == 0) {
            return suggestionDao.getStatPage(accountId, state, new Date(startTime), new Date(endTime));
        }
        return suggestionDao.getOperationStatPage(accountId, state, new Date(startTime), new Date(endTime));
    }

    @Override
    public List<OpSuggestionStatVO> getSuggestionStat(Integer accountId, Long startTime, Long endTime) {
        return suggestionDao.getSuggestionStat(accountId, new Date(startTime), new Date(endTime));
    }

    /**
     * 查询所有反馈数量
     * @param accountIds 区域idList
     * @return 数量
     */
    @Override
    public int queryAllCount(List<Integer> accountIds, Date startTime, Date endTime) {
        return suggestionDao.queryAllCount(accountIds, startTime, endTime);
    }

    @Override
    public List<Suggestion> selectByAccountIdList(Integer userId, List<Integer> accountIdList, Date startTime, Date endTime) {
        return suggestionDao.selectByAccountIdList(userId, accountIdList, startTime, endTime);
    }
}
